<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/const.php' );
require_once( '../../libs/page.php' );

$type = $_POST["type"];
$shop_name = $_POST["shop_name"];
$style = $_POST["style"];
$current=1;
if( isset( $_POST['page'] ) && is_numeric( $_POST['page'] ) ){
	$current = $_POST['page'];
	$st = ($current -1) * PAGE_SIZE;
}else{
	$current = 1;
	$st = 0;
}

$where = '';
$query = '';

if($type != null){
	$where = $where." and type like '%".$type."%'";
}
if($shop_name != null){
	$where = $where." and shop_name like '%".$shop_name."%'";
}
if($style != null){
	$where = $where." and style like '%".$style."%'";
}

$head = "商品コード,型番,商品名,仕様,参考価格,原価,単位,申請者,備考,更新日";

$sql =  "SELECT * FROM p_shop WHERE 1 = 1 $where order by u_date desc ";
$db = new Class_DB;
$res = $db->query($sql);
$data = array();

while( $row = $db->fetch() ){
	if($row['style']=="" && $row['style1']!=""){
		$row['style']=$row['style1']."mm×".$row['style2']."m";
	}

	
	$data[] = mb_convert_encoding( sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"",
			$row['shop_id'],
			$row['type'],
			$row['shop_name'],
			$row['style'],
			$row['refer_price'],
			$row['cost_price'],
			$row['s_unit'],
			$row['applicant'],
			$row['remark'],
			$row['u_date']
		),
		"SJIS-win",
		"utf-8"
	);
}
/*
while( $row = $db->fetch() ){
	$data[] = mb_convert_encoding( sprintf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"",
			$row['shop_id'],
			$row['type'],
			$row['shop_name'],
			$row['style'],
			$row['refer_price'],
			$row['cost_price'],
			$row['multiple'],
			$row['num'],
			$row['cost_price']*$row['num']*$row['multiple'],
			$row['s_unit'],
			$row['r_date'],
			$row['u_date'],
			$row['applicant'],
			$row['remark'],
			$row['special_reason']
		),
		"sjis",
		"utf8"
	);
}

*/

$filename = date("YmdHis") . '.csv';

$head = mb_convert_encoding( $head, "SJIS-win", "utf-8");

header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename=' . $filename );

print $head;
print "\n";
print implode( $data, "\n" );
?>